/**
 * 
 */
package com.gragra.wilsHandler;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Christoph Teichmann
 * 26.09.2013
 */
public class EvaluateDependencyMicroAveraged
{
	/**
	 * @param args
	 */
	public static double[] main(BufferedReader gold, BufferedReader eval) throws IOException
	{
		String g;
		String e;
		double seen = 0.0;
		double acc = 0.0;
		double unAcc = 0.0;
		while((g = gold.readLine()) != null)
		{
			g = gold.readLine();
			eval.readLine();
			e = eval.readLine();
			IntArrayList head = new IntArrayList();
			ArrayList<IntOpenHashSet> connected = new ArrayList<IntOpenHashSet>();
			String[] parts = e.trim().split("\\s+");
			for(int i=0;i<parts.length;++i)
			{
				connected.add(new IntOpenHashSet());
				head.add(Integer.parseInt(parts[i]));
			}
			for(int i=0;i<parts.length;++i)
			{
				int h = head.getInt(i);
				if(h > 0)
				{connected.get(h-1).add(i+1);}
				connected.get(i).add(h);
			}
			parts = g.split("\\s+");
			for(int i = 0; i < parts.length; i++)
			{
				int h = Integer.parseInt(parts[i]);
				++seen;
				acc += h==head.getInt(i)? 1.0 : 0.0;
				unAcc += connected.get(i).contains(h) ? 1.0 : 0.0;
			}
			
		}
		return new double[] {acc/seen,unAcc/seen};
	}
}